In this Article, I describe what is partial view in asp.net mvc. Partial views are used to encapsulate reusable view logic and it simplify the complexity of views. We can use one partial view on multilple views , where we need similar kind of view logics.
If you create a partial view in shared folder then you can access a partial view anywhere in your application but if you create partial view in specific folder or models with in that folder or modules only you can access your partial views.
Partial View creation is similar to normal view creation, but check Create as a partial view checkbox before add a partial view.
Render PartialView
@Html.Partial("Partial viewName",Model)
Example
@Html.Partial("_Product", item)
Here _Product is a Partial view name and item is a name of the model.
_Product View
==================
@model MVCApp.Models.Product
<table>
<tr>
<td>
@Model.Name
</td>
<td>
@Model.Description
</td>
</tr>
</table>
Index View
@model IEnumerable<Gnawazexport.Areas.Admin.Models.ProductImage>
@{
ViewBag.Title = "Index";
}
@foreach (var item in Model)
{
@Html.Partial("_Product", item)
}
Description
In this above demonstration, I used product as a partial view and use this partial in index view. Similarly we can use this partial wherever you need.
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article